package de.merten;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.CertificateException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.security.cert.X509Certificate;

// see http://code.google.com/p/misc-utils/wiki/JavaHttpsUrl
// 4 Credentials: http://stackoverflow.com/questions/10479434/server-returned-http-response-code-401-for-url-https
public class SocketClient2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
               
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    // TODO Auto-generated method stub
                    
                }
                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    // TODO Auto-generated method stub
                    
                }
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    // TODO Auto-generated method stub
                    return null;
                }
            } };
            
            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance( "SSL" );
            sslContext.init( null, trustAllCerts, new java.security.SecureRandom() );
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            
         // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
            
            // All set up, we can get a resource through https now:
            //String url = "https://h1892669.stratoserver.net/energiemanager/app";
            String url = "https://www.strato.de/";
            //String url = "https://www.nakov.com:2083/";
            final URLConnection urlCon = new URL(url).openConnection();
            // Tell the url connection object to use our socket factory which bypasses security checks
            ( (HttpsURLConnection) urlCon ).setSSLSocketFactory( sslSocketFactory );
            
            final InputStream input = urlCon.getInputStream();
            
            Reader reader = new InputStreamReader(input);
            while (true) {
                int ch = reader.read();
                if (ch==-1) {
                    break;
                }
                System.out.print((char)ch);
            }
            
//            
//            int c;
//            while ( ( c = input.read() ) != -1 ) {
//                System.out.println(input.toString());
//            }
            input.close();
        } catch ( final Exception e ) {
            e.printStackTrace();
        }

    }

}
